/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package inputAdapter;
import figures.Figure;
import figures.cartography.Avenue;
import figures.cartography.Building;
import figures.cartography.Limit;
import figures.cartography.River;
import figures.cartography.SpeedWay;
import figures.cartography.Square;
import figures.cartography.Street;
import figures.cartography.Train;
import figures.cartography.Water;
import geometry.Point;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import mapWriters.BaseMapWriter;
import mapWriters.DefaultMapWriter;
import mapWriters.MapWriter;



public class ImportarSantiago {

	public static void main(String[] args) throws FileNotFoundException {
		//MapWriter mw = new DefaultMapWriter();
		MapWriter mw = new BaseMapWriter();
		FileInputStream is = new FileInputStream("santiago.txt");
		BufferedReader r = new BufferedReader(new InputStreamReader(is));
		Point i = new Point(0,0);
		Point f = new Point(0,0);
		String l;
		try {
			int nAnterior = -1;
			Point fAnterior = new Point(-1,-1);
			Point[] puntos = null;
			String nombre = new String();
			String id = new String();
			String tipo = "";
			while((l = r.readLine())!=null){
				int n = parsearLinea(l, i, f);
				if(n != nAnterior || !i.equals(fAnterior)){
					if(puntos != null){
						Figure fig = obtenerFigura(puntos, tipo, nombre);
						if(fig!=null){
							fig.setId(Integer.parseInt(id));
							mw.addFigure(fig);
						}
						puntos = null;
					}
				}
				Point[] tmp;
				if(puntos == null){
					tmp = new Point[2];
					tmp[0] = new Point(i);
				}else{
					tmp = new Point[puntos.length+1];
				}
				if(puntos != null)
					for(int k=0;k<puntos.length;k++)
						tmp[k] = puntos[k];
				tmp[tmp.length-1]=new Point(f);
				puntos = tmp;
				nAnterior = n;
				nombre = l.split("\t")[1];
				id = l.split("\t")[0];
				tipo = l.split("\t")[2];
				fAnterior = new Point(f);
			}
			DataOutputStream dos = new DataOutputStream(new FileOutputStream("santiago.map"));
			mw.write(dos);
			dos.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static int parsearLinea(String l, Point i, Point f) {
		String[] ss = l.split("\t");
		i.x = Integer.parseInt(ss[3]);
		i.y = Integer.parseInt(ss[4]);
		f.x = Integer.parseInt(ss[5]);
		f.y = Integer.parseInt(ss[6]);
		return Integer.parseInt(ss[0]);
	}

	private static Figure obtenerFigura(Point[] puntos, String tipo, String nombre) throws IOException {
		if(tipo.equals("AVDA") || tipo.equals("CARR"))
			return new Avenue(puntos, nombre);
		if(tipo.equals("CALLE"))
			return new Street(puntos, nombre);
		return null;
		//PASEO
		//PBN
		//PSJE
		//PSN
		//PTE
		//RTDA
		/*
		 * new SpeedWay(puntos, nombre);
		 * new Train(puntos, nombre);
		 * new Water(puntos, nombre);
		 * new River(puntos, nombre);
		 * new Limit(puntos, nombre);
		 * new Building(puntos, nombre);
		 * new Square(puntos, nombre);
		 * 
		 */
	}

}
